var sysObj = {}
var groupObj = {}
$(function () {
  //toolbar权限控制
  var openFlag = permissions.pre_node_open
  var stopFlag = permissions.pre_node_stop

  getInsystem()
  getAllGroup()

  $('#systemId').change(function(){
    if ($(this).val()){
      getGroup($(this).find("option:selected").data('code'))
    }
  })

  var options = {
    url: api + "/comm/sysNode/page",
    updateUrl: "/pages/pre/group/edit",
    modalName: "接入服务节点管理",
    columns: [
      {
        title: "序号",
        formatter: function (value, row, index) {
          return $.table.serialNumber(index);
        }
      },
      {
        field: 'svrAddr',
        title: '服务地址'
      },
      {
        field: 'systemId',
        title: '项目名称',
        formatter: function (value, row, index) {
          return sysObj[value];
        }
      },
      {
        field: 'groupId',
        title: '服务名称',
        formatter: function (value, row, index) {
          return groupObj[value];
        }
      },
      {
        field: 'status',
        title: '状态',
        formatter: function (value, row, index) {
          return statusDict(value);
        }
      },
      {
        title: '操作',
        formatter: function (value, row, index) {
          var actions = [];
          if (row.status == 0 && openFlag) {
            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="startNode(\'' + row.statusId + '\')"><i class="fa fa-hand-pointer-o"></i>启动</a> ');
          }
          if (row.status == 1 && stopFlag) {
            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="stopNode(\'' + row.statusId + '\')"><i class="fa fa-hand-pointer-o"></i>停止</a> ');
          }
          return actions.join('');
        }
      }]
  };
  $.table.init(options);
});

//获取接入系统列表
function getInsystem(){
  $.operate.get(api + '/comm/insystem/insys_list', function (res) {
    res.data.forEach(function (item) {
      $('#systemId').append('<option value="' + item.sysId + '" data-code="'+ item.code +'">' + item.name + '</option>')
      sysObj[item.sysId] = item.name
    })
  }, false)
}

//获取分组列表
function getAllGroup(){
  $.operate.get(api + '/comm/group/group_list/all', function (res) {
    res.data.forEach(function (item) {
      groupObj[item.groupId] = item.name
    })
  }, false)
}

//根据接入系统名称获取分组列表
function getGroup(code){
  $('#groupId').find('option').remove()
  $('#groupId').append('<option value=""></option>')
  $.operate.get(api + '/comm/group/group_list/code/'+code, function (res) {
    res.data.forEach(function (item) {
      $('#groupId').append('<option value="' + item.groupId + '">' + item.name + '</option>')
    })
  }, false)
}

function statusDict(item){
  var dict = {
    '0': {text:'已停止',color:'label-danger'},
    '1': {text:'已启动' ,color:'label-success'},
  }
  return '<span class="label '+(dict[item] ? dict[item].color : '')+' list-label">'+(dict[item] ? dict[item].text : item)+'</span>'
}

function startNode(id) {
  $.operate.post(api + '/comm/sysNode/start/' + id, {}, function (res){
    if (res.code == 0){
      $.modal.msgSuccess('启动成功')
    }else{
      $.modal.msgError(res.msg ? res.msg : '错误！')
    }
  })
}

function stopNode(id) {
  $.operate.post(api + '/comm/sysNode/stop/' + id, {}, function (res){
    if (res.code == 0){
      $.modal.msgSuccess('停止成功')
    }else{
      $.modal.msgError(res.msg ? res.msg : '错误！')
    }
  })
}